<?php
/**
 * Created by PhpStorm.
 * User: liang
 * Email:1050187159@qq.com
 * Date: 2018/8/15
 * Time: 21:35
 */
namespace Admin\Model;

use Think\Model;

class OptionModel extends Model
{
    protected $trueTableName = 'options';

    public function optionsAdd()
    {
        $navigator_id = I('post.navigator_id');
        $option_name = I('post.option_name');
        $english_name = strtoupper(I('post.english_name'));
        $is_show = I('post.is_show');
        $option_image = I('post.option_image');
        $sort = I('post.sort');
        $description = I('post.description');

        $count = $this->where(array('option_name' => array('eq', $option_name)))->count();
        if ($count) {
            $this->error = '该导航名称已存在';
            return false;
        }
        if (empty($option_name)) {
            $this->error = '名称不能为空';
            return false;
        }
        if (mb_strlen($option_name) > 30) {
            $this->error = '名称长度不能超过30';
            return false;
        }
        if (empty($english_name)) {
            $this->error = '名称不能为空';
            return false;
        }
        if (mb_strlen($english_name) > 30) {
            $this->error = '名称长度不能超过30';
            return false;
        }
        if (empty($option_image)) {
            $this->error = '图片必须上传';
            return false;
        }
        if (empty($sort)) {
            $this->error = '排序不能为空';
            return false;
        }
        if (is_integer($sort)) {
            $this->error = '排序必须为整数';
            return false;
        }
        if (empty(trim(strip_tags(htmlspecialchars_decode($description))))) {
            $this->error = '描述不能为空';
            return false;
        }

        $this->navigator_id = $navigator_id;
        $this->option_name = $option_name;
        $this->english_name = $english_name;
        $this->is_show = $is_show;
        $this->option_image = $option_image;
        $this->sort = $sort;
        $this->description = $description;

        if ($this->add()) {
            return true;
        } else {
            return false;
        }
    }

    public function optionsModify()
    {
        $id = I('post.id');
        $navigator_id = I('post.navigator_id');
        $option_name = I('post.option_name');
        $english_name = strtoupper(I('post.english_name'));
        $is_show = I('post.is_show');
        $option_image = I('post.option_image');
        $sort = I('post.sort');
        $description = I('post.description');

        $where['option_name'] = array('eq', $option_name);
        $where['id'] = array('neq', $id);
        $count = $this->where($where)->count();
        if ($count != '0') {
            $this->error = '该导航名称已存在';
            return false;
        }
        if (empty($option_name)) {
            $this->error = '名称不能为空';
            return false;
        }
        if (mb_strlen($option_name) > 30) {
            $this->error = '名称长度不能超过30';
            return false;
        }
        if (empty($english_name)) {
            $this->error = '英文名称不能为空';
            return false;
        }
        if (mb_strlen($english_name) > 30) {
            $this->error = '英文名称长度不能超过30';
            return false;
        }
        if (empty($option_image)) {
            $this->error = '图片必须上传';
            return false;
        }
        if (empty($sort)) {
            $this->error = '排序不能为空';
            return false;
        }
        if (is_integer($sort)) {
            $this->error = '排序必须为整数';
            return false;
        }

        if (empty(trim(I('post.description','','strip_tags')))) {
            $this->error = '描述不能为空';
            return false;
        }


        $this->navigator_id = $navigator_id;
        $this->option_name = $option_name;
        $this->english_name = $english_name;
        $this->is_show = $is_show;
        $this->option_image = $option_image;
        $this->sort = $sort;
        $this->description = $description;

        if ($this->where(array('id' => array('eq', $id)))->save() === false) {
            $this->error = '修改失败,请重试';
            return false;
        } else {
            return true;
        }
    }
}
